import { Dispatch } from 'redux';
import type { IListData, IStateType } from './index.d';

export const MODULE_NAME: string = 'OnlineResearch/LiveBroad';

export const types = {
  DATA_SOURCE: `${MODULE_NAME}/DATA_SOURCE`, // 列表数据
}

export default (state: IStateType = {
  dataSource: []
}, action: any) => {
  switch (action.type) {
    case types.DATA_SOURCE:
      return Object.assign({}, state, {
        dataSource: action.data
      })
    default: return state
  }
}

/**
 * 获取列表数据
*/
export const getList = (keyword: string = '', tableKey: string = 'all') => {
  return async (dispatch: Dispatch): Promise<void> => {
    try {
      const data: IListData = [{
        title: '家乐园小学录播教室1',
        image: require('@/assets/images/live-broadcast.png'),
        id: '10001',
        key: '1',
      }, {
        title: '家乐园小学录播教室2',
        image: require('@/assets/images/live-broadcast.png'),
        id: '10002',
        key: '1',
      }, {
        title: '龙泉小学阶梯教室',
        image: require('@/assets/images/live-broadcast.png'),
        key: '2',
        id: '10003',
        camerId: 'eff369ee415941488840a2c05fe85547'
      }, {
        title: '龙泉小学一楼东1(教室)',
        image: require('@/assets/images/live-broadcast.png'),
        key: '2',
        id: '10004',
        camerId: 'be663af686b74f2a8882dc3d8dee07fa'
      }, {
        title: '东关逸夫小学录播教室1',
        image: require('@/assets/images/live-broadcast.png'),
        id: '10005',
        key: '3',
      }, {
        title: '东关逸夫小学录播教室2',
        image: require('@/assets/images/live-broadcast.png'),
        id: '10006',
        key: '3',
      }]

      dispatch({
        type: types.DATA_SOURCE,
        data: data.filter(item => item.title.includes(keyword) && (item.key === tableKey || tableKey === 'all'))
      })
    } catch { }
  }
}
